package com.heima.user.service.impl;

import com.heima.common.constants.BehaviorConstants;
import com.heima.common.schedule.CacheService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.service.UserService;
import com.heima.user.thread.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private CacheService cacheService;

    @Override
    public ResponseResult follow(UserRelationDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtil.getUser();
        Integer id = user.getId();
        if (id==null||id==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Short operation = dto.getOperation();
        Integer authorId = dto.getAuthorId();
        if (operation==0){
          cacheService.zAdd(BehaviorConstants.APUSER_FOLLOW_RELATION+id,authorId.toString(),System.currentTimeMillis());
          cacheService.zAdd(BehaviorConstants.APUSER_FANS_RELATION+authorId,id.toString(),System.currentTimeMillis());
        }else {
            cacheService.zRemove(BehaviorConstants.APUSER_FOLLOW_RELATION+id,authorId.toString());
            cacheService.zRemove(BehaviorConstants.APUSER_FANS_RELATION+authorId,id.toString());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
